<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.com/license/
 * @version    $Id: Controller.php 9747 2012-07-26 02:08:08Z john $
 * @author     John
 */

/**
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.com/license/
 */
class User_Widget_ListOnlineController extends Engine_Content_Widget_Abstract
{
  protected $_onlineUserCount;
  
  public function indexAction()
  {
   $headScript = new Zend_View_Helper_HeadScript();
    $headScript->appendFile(Zend_Registry::get('StaticBaseUrl') 
        . 'application/modules/Icustom/externals/scripts/core.js');
    // Get online users
    $table = Engine_Api::_()->getItemTable('user');
    $onlineTable = Engine_Api::_()->getDbtable('online', 'user');
    
    $tableName = $table->info('name');
    $onlineTableName = $onlineTable->info('name');

    //get users online from chat
    $select1 = $table->select()
      ->from($tableName)
      ->join('visichat_online_users', 'visichat_online_users.visi_online_userid = '.$tableName.'.user_id', null);
	$aUsers = $table->fetchAll($select1);
	
	$ids = array();
    foreach ($aUsers as $user){
    	$ids[] = $user->getIdentity();
    }

    $select = $table->select()
      //->from($onlineTableName, null)
      //->joinLeft($tableName, $onlineTable.'.user_id = '.$tableName.'.user_id', null)
      ->from($tableName)
      ->joinLeft($onlineTableName, $onlineTableName.'.user_id = '.$tableName.'.user_id', null)
	  //->where($tableName.'.user_id IN(?)', $ids)
      //->orWhere($onlineTableName.'.user_id > 0 AND ' .$onlineTableName.'.active > ' .new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 1 MINUTE)').' AND '. $tableName.'.search = 1 AND '.$tableName.'.enabled = 1 AND '.$tableName.'.user_id NOT IN(?)', $ids)// GROUP BY '. $onlineTableName.'.user_id ORDER BY '.$onlineTableName.'.active DESC');
      //->where($onlineTableName.'.active > ?', new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 20 MINUTE)'))
      //->where($tableName.'.search = ?', 1)
      //->where($tableName.'.enabled = ?', 1)
      ->order($onlineTableName.'.active DESC')
      ->group($onlineTableName.'.user_id')
      ;
     if(count($ids)){
     	$select->where($tableName.'.user_id IN(?)', $ids)
      		  ->orWhere($onlineTableName.'.user_id > 0 AND ' .$onlineTableName.'.active > ' .new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 1 MINUTE)').' AND '. $tableName.'.search = 1 AND '.$tableName.'.enabled = 1 AND '.$tableName.'.user_id NOT IN(?)', $ids);// GROUP BY '. $onlineTableName.'.user_id ORDER BY '.$onlineTableName.'.active DESC');
     }else{
     	$select->where($onlineTableName.'.user_id > 0')
     		->where($onlineTableName.'.active > ?', new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 20 MINUTE)'))
      		->where($tableName.'.search = ?', 1)
      		->where($tableName.'.enabled = ?', 1);
     } 
     
	//echo $select;
    $paginator = Zend_Paginator::factory($select);

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->view->itemCountPerPage = $this->_getParam('itemCountPerPage', 16));
    $paginator->setCurrentPageNumber($this->view->page = $this->_getParam('page', 1));

    // Skip if empty
    //$count = $paginator->getTotalItemCount();    
	$count = count($table->fetchAll($select));
      
    if( $count <= 0 ) {
//      return $this->setNoRender();
    }
    $this->view->paginator = $paginator;

    // Make title
    $this->_onlineUserCount = $count;
    
    $element = $this->getElement();
    $title = $this->view->translate(array($element->getTitle(), $element->getTitle(), $count), $this->view->locale()->toNumber($count));
    $element->setTitle($title);
    $element->setParam('disableTranslate', true);

    // Guests online
    $this->view->guestCount = null;
    if( $this->_getParam('showGuests', true) ) {
      $this->view->guestCount = $onlineTable->select()
        ->from($onlineTable, new Zend_Db_Expr('COUNT(*) as count'))
        ->where('user_id = ?', 0)
        ->where('active > ?', new Zend_Db_Expr('DATE_SUB(NOW(),INTERVAL 20 MINUTE)'))
        ->query()
        ->fetchColumn();
        ;
    }
  }

//  public function getCacheKey()
//  {
//    $viewer = Engine_Api::_()->user()->getViewer();
//    $translate = Zend_Registry::get('Zend_Translate');
//    return $viewer->getIdentity() . $translate->getLocale();
//  }
//
//  public function getCacheSpecificLifetime()
//  {
//    return 120;
//  }
//
//  public function getCacheExtraContent()
//  {
//    return $this->_onlineUserCount;
//  }
//
//  public function setCacheExtraData($data)
//  {
//    $element = $this->getElement();
//    $element->setTitle(sprintf($element->getTitle(), (int) $data));
//  }
}